#include "process.h"
#include <string.h>
#include <unistd.h>

#define LENGTH 101
#define LABLE '#'

void Process()
{
    const char *symb = "|/-\\";
    int s_len = 4;
    char bar[LENGTH];
    memset(bar, '\0', sizeof(bar));
    int cnt = 0;
    while(cnt <= 100)
    {
        printf("[%-100s][%d%%][%c]\r", bar, cnt, symb[cnt % s_len]);
        bar[cnt++] = LABLE;
        fflush(stdout);
        usleep(30000);
    }
    printf("\n");
}

void FlushProcess(double target, double current)
{
   if(target <= current) 
   {
       static int index = 0;
       const char *symb = "|/-\\";
       char bar[LENGTH];
       memset(bar, '\0', sizeof bar);
       for(int i = 0; i < 100; i++)
       {
           bar[i] = LABLE;
       }
       printf("[%-100s][100.0%%][%c]\r", bar, symb[index++]);
       return;
   }
   static int index = 0;
   const char *symb = "|/-\\";
   double rate =  (current / target) * 100.0;
   int cnt = (int)rate;
   char bar[LENGTH];
   memset(bar, '\0', sizeof bar);
   for(int i = 0; i < cnt; i++)
   {
       bar[i] = LABLE;
   }
   printf("[%-100s][%.1lf%%][%c]\r", bar, rate, symb[index++]);
   index %= 4;
   fflush(stdout);
}
